import {
  request_post,
  request_get,
  request_put,
  request_delete,
  request_file
} from "services/common/request";

import pathToRegexp from "path-to-regexp";
let toPathGroup = pathToRegexp.compile("/api/data_articles/groups/:id");
let ispord = process.env.NODE_ENV == "production";
let rtsp = ispord ? process.env.VUE_APP_RTSP_URL : '/rtsp';
// let rtsp = process.env.VUE_APP_RTSP_URL;

/**
 * 获取用户列表
 * @param {Object} data
 * @param {String} data.name - 选填 名称
 * @param {String} data.pageStart
 * @param {String} data.pageOffset
 * @param {String} data.pageNum
 */
export function getList(data = {}) {
  return request_get("/equipment/pageInfoBy", data);
}

/**
 * 获取用户
 * @param {Object} data
 * @param {String} data.name - 选填 名称
 * @param {String} data.username
 * @param {String} data.password
 */
export async function save(data = {}) {
  if (data.file) {
    data.cover = await request_file("/upload/uploadTempfile", {
      file: data.file,
      data
    }).then(res => {
      return res.data;
    });
  } else {
    data.cover = null;
  }
  if (data.id) {
    return request_post("/equipment/update", data);
  }
  return request_post("/equipment/saveStream", data);
}

/**
 * 获取详情
 * @param {Object} data
 * @param {String} data.name - 选填 名称
 * @param {String} data.username
 * @param {String} data.password
 */
export function detail(data = {}) {
  return request_get("/user/modifyPasswd", data);
}

/**
 * 删除
 * @param {Object} data
 * @param {String} data.name - 选填 名称
 * @param {String} data.username
 * @param {String} data.password
 */
export function del(data = {}) {
  return request_post("/equipment/delete", data);
}

/**
 * 实时监控数据展示
 * @param {Object} data
 * @param {String} data.id - 选填 名称
 */
export function info(data = {}) {
  return request_get("/equipment/realTimeInfo", data);
}

/**
 * 上、下架
 * @param {Object} data
 * @param {String} data.name - 选填 名称
 * @param {String} data.type
 * @param {String} data.password
 */
export function pull(id, type) {
  if (type) {
    return request_post("/sdk/start", { equipmentId: id, sdkId: null });
  }
  return request_post("/sdk/stop", { equipmentId: id, sdkId: null });
}

/**
 * 订阅与取消订阅
 * @param {Object} data
 * @param {String} data.name - 选填 名称
 * @param {String} data.type
 * @param {String} data.password
 */
export function subscribe(id, type) {
  if (type) {
    return request_post("/sdk/subscription", { equipmentId: id, sdkId: null });
  }
  return request_post("/sdk/deleteSubscription", { equipmentId: id, sdkId: null });
}

/**
 * 订阅与取消订阅
 * @param {Object} data
 * @param {String} data.name - 选填 名称
 * @param {String} data.type
 * @param {String} data.password
 */
export function refresh(id, type) {
  return request_post("/sdk/refreshSubscription", { equipmentId: id, sdkId: null });
}

/**
 * 启动本地推流服务器
 * @param {Object} data
 * @param {String} data.url - 必填 rtsp 地址
 */
export function start(data) {
  return request_post(`${rtsp}/port`, data);
}

/**
 * 关闭本地推流服务器
 * @param {Object} data
 */
export function close(data) {
  return request_get(`${rtsp}/port/stop`, data);
}
